﻿using IM.Easy.Core.Jobs;
using IM.Easy.Entity.Log;
using IM.Easy.LogServices.IServices;
using Quartz;
using SqlSugar;

namespace IM.Easy.Api.Jobs
{
    /// <summary>
    /// 日志清理定时任务
    /// </summary>
    public class ClearLogJob : IJobDefinition
    {
        public static string JobName => "清理日志";

        private readonly IRequestTrackingService _requestTrackingService;

        public ClearLogJob(IRequestTrackingService requestTrackingService)
        {
            _requestTrackingService = requestTrackingService;
        }



        public Task Execute(IJobExecutionContext context)
        {
            var threshold = DateTime.Now.Date.AddDays(-10);
            var exp = Expressionable.Create<RequestTrackingLog>();
            exp.And(s => s.CreatedAt <= threshold);
            _requestTrackingService.Delete(exp.ToExpression());
            Console.WriteLine($"日志清理成功,当前时间{DateTime.Now}");
            return Task.CompletedTask;
        }
    }
}
